package com.example.tx_player;

import static com.tencent.rtmp.TXLiveConstants.PLAY_EVT_PLAY_BEGIN;
import static com.tencent.rtmp.TXLiveConstants.PLAY_EVT_PLAY_END;
import static com.tencent.rtmp.TXLiveConstants.PLAY_EVT_PLAY_PROGRESS;
import static com.tencent.rtmp.TXLiveConstants.PLAY_EVT_VOD_PLAY_PREPARED;
import static com.tencent.rtmp.TXLiveConstants.RENDER_MODE_ADJUST_RESOLUTION;
import static com.tencent.rtmp.TXLiveConstants.RENDER_MODE_FULL_FILL_SCREEN;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.talon.lib_base.ConfigApi;
import com.tencent.rtmp.ITXVodPlayListener;
import com.tencent.rtmp.TXVodPlayer;
import com.tencent.rtmp.ui.TXCloudVideoView;

public class TxPlayerActivity extends AppCompatActivity {
    TXCloudVideoView mPlayerView;
    TXVodPlayer mVodPlayer;
    private String TAG = "腾讯播放器~~";
    private long time = 0;
    private SurfaceView surfaceView;
    ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tx_player);
        surfaceView = findViewById(R.id.surfaceView);
        String url = ConfigApi.videoUrl;
        //mPlayerView 即步骤2中添加的视频渲染 view
         mPlayerView = findViewById(R.id.txVideoView);
        // mPlayerView.setRenderMode(RENDER_MODE_FULL_FILL_SCREEN);
//创建 player 对象
        mVodPlayer = new TXVodPlayer(this);
//关联 player 对象与视频渲染 view
        // mVodPlayer.setPlayerView(mPlayerView);


        //硬件加速
        // mVodPlayer.stopPlay(true);

        //   mVodPlayer.enableHardwareDecode(true);
        time = System.currentTimeMillis();
        mVodPlayer.startPlay(url);


        mVodPlayer.setVodListener(new ITXVodPlayListener() {
            @Override
            public void onPlayEvent(TXVodPlayer txVodPlayer, int i, Bundle bundle) {
                switch (i) {
                    case PLAY_EVT_VOD_PLAY_PREPARED:
                        // playerStatusChanged(TxVodStatus.TX_VIDEO_PLAYER_STATUS_PREPARED);
                        float result = System.currentTimeMillis() - time;
                        Log.i(TAG, "[onPlayEvent] , startOnPrepare mVodPlayer " + mVodPlayer.hashCode() + " mUrl " + url);
                        Log.i("xiaote", TAG + "onPrepared time :" + result);
                        break;
                    case PLAY_EVT_PLAY_BEGIN:
                        Log.i(TAG, "[onPlayEvent] , PLAY_EVT_PLAY_BEGIN，" + mVodPlayer.hashCode() + ",url " + url);
                        break;
                    case PLAY_EVT_PLAY_PROGRESS:
                        break;
                    case PLAY_EVT_PLAY_END:
                        break;
                    default:
                        break;

                }
            }

            @Override
            public void onNetStatus(TXVodPlayer txVodPlayer, Bundle bundle) {

            }
        });

        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mVodPlayer.setSurface(holder.getSurface());
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                mVodPlayer.setSurface(null);
            }
        });

        findViewById(R.id.res).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  plVideoView.stop();
                mVodPlayer.stopPlay(true);
                mVodPlayer.startPlay(ConfigApi.videoUrl2);
                time = System.currentTimeMillis();

            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        mVodPlayer.stopPlay(true);
        mPlayerView.onDestroy();
    }
}